<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\DynamicInforMation;
use App\Services\AliyunUpload;
use Illuminate\Http\Request;

class HousesCircle extends Controller
{
    /**
     * @param Request $request
     * @return array
     * 楼市圈图片上传
     */
    public function uploadFile(Request $request)
    {
        //获取图片名称
        $path=$request->file('file')->getPathname();
        //获取图片后缀名
        $type=$request->file('file')->getClientOriginalExtension();
        //阿里云上传
        $uploadRes = AliyunUpload::upFile($path,$type);
        //判断是否上传成功
        if (!$uploadRes){
            return ['code'=>10004,'msg'=>'上传失败','data'=>[]];
        }
        return ['code'=>10001,'msg'=>'上传成功','data'=>['url'=>$uploadRes]];
    }

    /**
     * @param Request $request
     * @return array
     * 经纪人发布动态
     */
    public function issueDynamic(Request $request)
    {
        //处理多图数据
        $dynamic_imgs = implode(',',$request->get('dynamic_imgs'));
        //接收经纪人id
        $intermediary_id = $request->get('id');
        //动态标题
        $title = $request->get('title');
        //动态数据拼接
        $addData = ['intermediary_id'=>$intermediary_id,'title'=>$title,'dynamic_imgs'=>$dynamic_imgs,'issue_time'=>date('Y-m-d h:i:s',time())];
        //动态入库
        $issueRes = DynamicInforMation::addDynamic($addData);
        if (!$issueRes){
            return ['code'=>10004,'msg'=>'添加失败','data'=>[]];
        }
        return ['code'=>10001,'msg'=>'添加成功','data'=>[]];
    }

    /**
     * @param Request $request
     * @return array
     * 楼市圈列表
     */
    public function getDynamicList(Request $request)
    {
        //接收参数
        $user_id = $request->get('id');
        //获取楼市圈数据
        $data = DynamicInforMation::getDynamicData($user_id);
        //返回数据
        return ['code'=>10001,'msg'=>'请求成功','data'=>$data];
    }

    /**
     * @param Request $request
     * @return array
     * 删除楼市圈动态
     */
    public function deleteDynamic(Request $request)
    {
        //接收动态id
        $dynamic_id = $request->get('dynamic_id');
        //删除数据
        $deleteRes = DynamicInforMation::deleteData($dynamic_id);
        //判断删除结果
        if (!$deleteRes){
            return ['code'=>10004,'msg'=>'删除失败','data'=>[]];
        }
        return ['code'=>10001,'msg'=>'删除成功','data'=>[]];
    }
}
